<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addPermission(0, 1)">添加权限</el-button>
      <el-table :data="list" default-expand-all row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button v-if="row.type === 1" size="mini" type="text" @click="addPermission(row.id, 2)">添加</el-button>
            <el-button size="mini" type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增编辑权限的弹层 -->
    <el-dialog :visible="showDialog" :title="`${showText}权限点`" @close="btnCancel">
      <el-form ref="perForm" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启" prop="enVisible">
          <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnOk">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { transListToTresData } from '@/utils'
import { getPermissionList, addPermission, getPermissionDetail, updatePermission, delPermission } from '@/api/permission'
export default {
  name: 'Permission',
  data() {
    return {
      // 渲染列表
      list: [],
      // 表格列表
      formData: {
        name: '', // 权限名字
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 权限点类型
        pid: '', // 权限点父级id
        enVisible: 0 // 权限点开启状态
      },
      // 校验
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '权限名称的长度为2-10个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '权限标识的长度为2-10个字符', trigger: 'blur' }
        ]
      },
      showDialog: false // 弹层
    }
  },
  computed: {
    // 标题
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },

  created() {
    this.getPermissionList()
  },
  methods: {
    // 渲染
    async getPermissionList() {
      const res = transListToTresData(await getPermissionList(), 0)
      console.log('222', res)
      this.list = res
    },

    // 新增权限
    async addPermission(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },

    // 编辑权限
    async editPermission(id) {
      // 根据获取id获取详情
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    },

    // 删除权限
    async delPermission(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        this.getPermissionList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    },

    // 确认按钮
    async btnOk() {
      this.$refs.perForm.validate(async valid => {
        if (!valid) return
        if (this.formData.id) {
          await updatePermission(this.formData)
          this.$message.success('编辑权限成功')
        } else {
          await addPermission(this.formData)
          this.$message.success('添加权限成功')
        }
        this.getPermissionList()
        this.btnCancel()
      })
    },

    // 取消
    btnCancel() {
      this.formData = {
        name: '', // 权限名字
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 权限点类型
        pid: '', // 权限点父级id
        enVisible: 0 // 权限点开启状态
      }
      this.$refs.perForm.resetFields() // 重置表单
      this.showDialog = false
    }

  }
}
</script>
<style scoped lang="scss">
.btn-add {
  margin: 10px;
}
</style>
